package com.yehun.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yehun.admin.controller.BaseController;
import com.yehun.dao.RouteDao;
import com.yehun.entity.PagerEntity;
import com.yehun.entity.Route;
import com.yehun.model.MappingModel;
import com.yehun.service.RouteService;
import com.yehun.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class RouteServiceImpl implements RouteService {

    private final static Logger logger = LoggerFactory.getLogger(RouteServiceImpl.class);

    @Resource
    private RouteDao routeDao;

    @Override
    public void initialize() {
        logger.info("initialize mapping url, begin...");
        List<MappingModel> mappings = PackageUtils.getRequestMappings(BaseController.class);
        if (mappings != null && !mappings.isEmpty()) {
            int row = 0;
            for (MappingModel mapping : mappings) {
                if (StringUtils.isEmpty(mapping.getName())) continue;
                Route route = new Route();
                if (!StringUtils.isEmpty(mapping.getUrl())) {
                    String lastChar = mapping.getUrl().substring(mapping.getUrl().length() - 1, mapping.getUrl().length());
                    if (Objects.equals(lastChar, "/")) {
                        mapping.setUrl(mapping.getUrl().substring(0, mapping.getUrl().length() - 1));
                    } else {
                        mapping.setUrl(mapping.getUrl());
                    }
                }
                route.setUrl(mapping.getUrl());
                route.setPath(mapping.getPath());
                route.setRemark(mapping.getName());
                row += this.merge(route);
            }
            logger.info("initialize mapping url, row={}", row);
        }
        logger.info("initialize mapping url, end...");
    }

    @Override
    public List<Route> listAll() {
        return routeDao.listAll();
    }

    @Override
    public PagerEntity<Route> listPager(int pageIndex, int pageSize) {
        PagerEntity<Route> pager = new PagerEntity<>();
        Page page = PageHelper.startPage(pageIndex, pageSize);
        pager.setData(routeDao.listAll());
        pager.setTotal(page.getTotal());
        pager.setPageIndex(pageIndex);
        pager.setPageSize(pageSize);
        return pager;
    }

    @Override
    public Route get(int id) {
        return routeDao.get(id);
    }

    @Override
    public Route getByUrl(String url) {
        return routeDao.getByUrl(url);
    }

    @Override
    public int save(Route route) {
        return routeDao.save(route);
    }

    @Override
    public int update(Route route) {
        return routeDao.update(route);
    }

    @Override
    public int merge(Route route) {
        int row;
        Route entity = routeDao.getByUrl(route.getUrl());
        if (entity == null) {
            row = routeDao.save(route);
        } else {
            row = routeDao.updateByUrl(route);
        }
        return row;
    }

    @Override
    public int remove(Integer[] idArray) {
        return routeDao.remove(idArray);
    }

}
